(in-package :com.freizl.common-util)

;;; transform each digit of a number to a list
(defun digits (num)
  (map 'list
       (lambda (char) (read-from-string (string char)))
       (prin1-to-string num)))

;;;
(defun factorial (n)
  (if (<= n 1) 1
      (* n (factorial (1- n)))))

;;; all divisors of n
(defun list-divisors (n)
  (loop for i from 1 to (sqrt n)
        when (= (mod n i) 0)
        collect i
        and collect (/ n i)))

(defun sum-of-restricted-divisors (n)
  (- (reduce #'+ (list-divisors n)) n))

;;;
(defun range (start end)
  (loop for i from start below end collect i))

;;; fibonacci
(defun fibonacci (seq)
  (loop repeat seq
        for x = 0 then y
        and y = 1 then (+ x y)
        collect y))

(defun fibonacci-list (upper-limit)
  (loop for x = 0 then y
        and y = 1 then (+ x y)
        while (< y upper-limit)
        collect y))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(format t "~a~%" (time (digits 1234)))
